package com.css.dzj.dzjportal.modules.supervision.controller;


import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjportal.common.util.BeanUtil;
import com.css.dzj.dzjportal.common.util.Constant;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.common.util.ServiceUtil;
import com.css.dzj.dzjportal.modules.supervision.entity.SupervisionMatterEntity;
import com.css.dzj.dzjportal.modules.supervision.service.SupervisionService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 监管事项清单controller
 */
@RestController
@RequestMapping("/supervision")
public class SupervisionController {

    @Autowired
    private SupervisionService supervisionService;

    @PostMapping("/zcadd")
    public R zcadd(@RequestBody SupervisionMatterEntity supervisionMatterEntity, HttpServletRequest request) {
        try {
            if (StringUtils.isEmpty(supervisionMatterEntity)) {
                return R.error("监管事项清单不能为空");
            }
            String loginName = ServiceUtil.getLoginName(request);
            if(StringUtils.isEmpty(loginName)) {
                return R.error("登录信息异常");
            }
            supervisionMatterEntity.setCreatePerson(loginName);
            supervisionMatterEntity.setCreateTime(new Date());
            supervisionMatterEntity.setDelFlag(Constant.NO_DELETE);
            // 0：待公开 1：已公开
            supervisionMatterEntity.setStatus("0");
            supervisionMatterEntity.setPublicLocation("工作门户");
            supervisionService.add(supervisionMatterEntity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 监管事项清单生效--（针对新建清单）
     * @param supervisionMatterEntity
     * @param request
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody SupervisionMatterEntity supervisionMatterEntity, HttpServletRequest request) {
        try {
            if(StringUtils.isEmpty(supervisionMatterEntity)) {
                return R.error("监管事项清单不能为空");
            }
            /*if(StringUtils.isEmpty(supervisionMatterEntity.getIsPublic())) {
                return R.error("请选择是否公开");
            }
            if("1".equals(supervisionMatterEntity.getIsPublic())) {
                return R.error("该监管事项无法公开");
            }*/
            String loginName = ServiceUtil.getLoginName(request);
            if(StringUtils.isEmpty(loginName)) {
                return R.error("登录信息异常");
            }
            supervisionMatterEntity.setCreatePerson(loginName);
            supervisionMatterEntity.setCreateTime(new Date());
            supervisionMatterEntity.setPublicLocation("工作门户");
            supervisionMatterEntity.setDelFlag(Constant.NO_DELETE);
            // 0：待公开 1：已公开
            supervisionMatterEntity.setStatus("1");
            supervisionService.add(supervisionMatterEntity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/queryById")
    public R queryById(@RequestParam String id) {
        try {
            if(StringUtils.isEmpty(id)) {
                return R.error("id不能为空");
            }
            SupervisionMatterEntity supervisionMatterEntity = supervisionService.queryById(id);
            return R.ok().put("data", supervisionMatterEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 修改监管事项清单
     * @param supervisionMatterEntity
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody SupervisionMatterEntity supervisionMatterEntity, HttpServletRequest request) {
        try {
            if(StringUtils.isEmpty(supervisionMatterEntity)) {
                return R.error("监管事项清单不能为空");
            }
            SupervisionMatterEntity oldEntity = supervisionService.queryById(supervisionMatterEntity.getId());
            if(StringUtils.isEmpty(oldEntity)) {
                return R.error("该监管事项清单不存在");
            }
            BeanUtil.copyBeanNotEmptyToBean(supervisionMatterEntity, oldEntity);
            String loginName = ServiceUtil.getLoginName(request);
            if(StringUtils.isEmpty(loginName)) {
                return R.error("登录信息异常");
            }
            oldEntity.setUpdatePerson(loginName);
            oldEntity.setUpdateTime(new Date());
            oldEntity.setStatus("0");
            supervisionService.update(oldEntity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 逻辑删除监管事项清单
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public R delete(@RequestParam String id) {
        try {
            if(StringUtils.isEmpty(id)) {
                return R.error("id不能为空");
            }
            SupervisionMatterEntity supervisionMatterEntity = supervisionService.queryById(id);
            if (StringUtils.isEmpty(supervisionMatterEntity)) {
                return R.error("该监管事项清单不存在");
            }
            supervisionMatterEntity.setDelFlag(Constant.DELETE);
            supervisionService.update(supervisionMatterEntity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 分页查询所有监管事项清单
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/getPageAll")
    public R getPageAll(@RequestParam(name = "pageSize") Integer pageSize,
                        @RequestParam(name = "pageNo") Integer pageNo) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<SupervisionMatterEntity> results = supervisionService.getPageAll(pageRequest);
            return R.ok().put("data", results);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 多条件分页查询监管事项清单
     * @param supervisionNumber
     * @param supervisionName
     * @param supervisionObject
     * @param permitType
     * @param supervisionForm
     * @param publicLocation
     * @param status
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/listPage")
    public R listPage(@RequestParam(name = "supervisionNumber" ,required = false) String supervisionNumber,
                      @RequestParam(name = "supervisionName",required = false) String supervisionName,
                      @RequestParam(name = "supervisionObject",required = false) String supervisionObject,
                      @RequestParam(name = "permitType",required = false) String permitType,
                      @RequestParam(name = "supervisionForm",required = false) String supervisionForm,
                      @RequestParam(name = "publicLocation",required = false) String publicLocation,
                      @RequestParam(name = "status",required = false) String status,
                      @RequestParam(name = "pageSize",required = false) Integer pageSize,
                      @RequestParam(name = "pageNo",required = false) Integer pageNo) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<SupervisionMatterEntity> results = supervisionService.listPage(supervisionNumber, supervisionName, supervisionObject, permitType, supervisionForm, publicLocation, status, pageRequest);
            return R.ok().put("data", results);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 生效--针对已经存在的
     * @param id
     * @return
     */
    @PostMapping("/effect")
    public R effect(@RequestParam String id) {
        try {
            if(StringUtils.isEmpty(id)) {
                return R.error("id不能为空");
            }
            SupervisionMatterEntity entity = supervisionService.queryById(id);
            if(StringUtils.isEmpty(entity)) {
                return R.error("该监管事项清单不存在");
            }
            /*if(StringUtils.isEmpty(entity.getIsPublic())) {
                return R.error("请选择是否公开");
            }
            if("1".equals(entity.getIsPublic())) {
                return R.error("该监管事项无法公开");
            }*/
            // 状态改为已公开
            entity.setStatus("1");
            supervisionService.update(entity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 撤销---(已公开的)
     * @param id
     * @return
     */
    @PostMapping("/revoke")
    public R revoke(@RequestParam String id) {
        try {
            if(StringUtils.isEmpty(id)) {
                return R.error("id不能为空");
            }
            SupervisionMatterEntity entity = supervisionService.queryById(id);
            if(StringUtils.isEmpty(entity)) {
                return R.error("该监管事项清单不存在");
            }
            // 状态改为已撤销
            entity.setStatus("2");
            supervisionService.update(entity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    @GetMapping("/queryPublic")
    public R queryPublic(@RequestParam(required = false) String supervisionName,
                         @RequestParam(required = true) String publicLocation,
                         @RequestParam(required = true) Integer pageSize,
                         @RequestParam(required = true) Integer pageNo) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<SupervisionMatterEntity> results = supervisionService.queryPublic(supervisionName, publicLocation, pageRequest);
            return R.ok().put("data", results);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

  /**
    *@Author weitao
    *@Date 10:36 2020/4/29
     * 多条件查询监管事项清单
    */
    @GetMapping("/wtlistPage")
    public R wtlistPage(@RequestParam(name = "supervisionNumber") String supervisionNumber,
                        @RequestParam(name = "supervisionName") String supervisionName,
                        @RequestParam(name = "supervisionSubItem") String supervisionSubItem,
                        @RequestParam(name = "supervisionDept") String supervisionDept,
                        @RequestParam(name = "permitName") String permitName,
                        @RequestParam(name = "permitType") String permitType,
                        @RequestParam(name = "pageSize") Integer pageSize,
                        @RequestParam(name = "pageNo") Integer pageNo) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<SupervisionMatterEntity> wtresults = supervisionService.wtlistPage(supervisionNumber, supervisionName, supervisionSubItem, supervisionDept, permitName, permitType, pageRequest);
            return R.ok().put("data", wtresults);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
    *@Author weitao
    *@Date 13:56 2020/4/29
     * 根据参数导出监管事项清单
    */
    @GetMapping("/export")
    public R export(HttpServletResponse response,
                    @RequestParam(required = false) String supervisionNumber,
                    @RequestParam(required = false) String supervisionName,
                    @RequestParam(required = false) String supervisionSubItem,
                    @RequestParam(required = false) String supervisionDept,
                    @RequestParam(required = false) String permitName,
                    @RequestParam(required = false) String permitType) {
        try {
            List<SupervisionMatterEntity> superList = supervisionService.superList(supervisionNumber,supervisionName,supervisionSubItem,supervisionDept,permitName,permitType);
            supervisionService.exportSuperToExcel(response, superList);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
    *@Author weitao
    *@Date 14:13 2020/4/29
     * 导出选中行数据 通过ids
    */
    @GetMapping("/exportByIds")
    public R exportByIds(HttpServletResponse response, @RequestParam String ids) {
        try {
            List<SupervisionMatterEntity> superList = supervisionService.queryByIds(ids);
            supervisionService.exportSuperToExcel(response, superList);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
    *@Author weitao
    *@Date 14:39 2020/4/29
     * 统计数量
    */
    @GetMapping("/superCount")
    public R superCount(){

        try {
            String superCount = supervisionService.getSupervCount();
            return R.ok().put("data",superCount);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

}
